<?php

class HomeController extends MController {

    public function indexAction() {
        __import('jquery');
        __import('jquery-form');
        __import('jquery-colorbox');
        __template()->addScript(__publish($this->getTemplatePath() . DS . 'js_scripts' . DS . 'ml.microblog.post.js'));
        $postForm = new CreatePostForm();
        $postForm->setId('createPostForm');
        $popularTags = Tag::all(array('limit' => '8'));
        $searchForm = new SearchForm();
        $this->assign('searchForm', $searchForm);
        $this->assign('mlSidebar', $this->renderPartial('sidebar', array('popularTags' => $popularTags), true));
        $this->render('index', array('postForm' => $postForm));
    }

    public function loginAction() {
        $form = new LoginForm();
        if ($this->getRequest()->isAjax()) {
            if ($this->getRequest()->isPost()) {
                $postData = $this->getRequest()->getPost();
//                if (User::exists(array('login'=>$postData['loginName']))){
//                    $user = User::find(array('login'=>$postData['loginName']));
//                    if ($user->password == sha1($postData['loginPassword'])){
                MSession::getInstance()->login(MSession::NORMAL_AUTH, $postData['loginName'], $postData['loginPassword']);
                __redirect(__route('ml-microblog'));
//                    }
//                }
            }
            $this->renderPartial('login', array('form' => $form));
        } else {
            if ($this->getRequest()->isPost()) {
                $postData = $this->getRequest()->getPost();
                MSession::getInstance()->login(MSession::NORMAL_AUTH, $postData['loginName'], $postData['loginPassword']);
                __redirect(__route('ml-microblog'));
            }
            $this->render('login', array('form' => $form));
        }
    }

    public function signoutAction() {
        MSession::getInstance()->logout();
        __redirect(__route('ml-microblog'));
    }

    public function signupAction() {
        $form = new SignupForm();
        if ($this->getRequest()->isAjax()) {
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($this->getRequest()->getPost())) {
                    try {
                        $postData = $this->getRequest()->getPost();

                        $return = MSession::getInstance()->register($postData['signupLogin'], $postData['signupPassword'], $postData['signupMail']);

                        if ($return == MSession::REGISTER_SUCCESS) {
                            $profile = new Profile();
                            $profile->active = 1;
                            $profile->user_id = MSession::getInstance()->getUserID();
                            $profile->save();

                            __redirect(__route('ml-microblog'));
                        }
                    } catch (Exception $ex) {
                        $form->addError($ex->getMessage());
                        $form->bindData($this->getRequest()->getPost());
                    }
                } else {
                    $form->bindData($this->getRequest()->getPost());
                }
            }
            $this->renderPartial('signup', array('form' => $form));
        } else {
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($this->getRequest()->getPost())) {
                    try {
                        $postData = $this->getRequest()->getPost();

                        $return = MSession::getInstance()->register($postData['signupLogin'], $postData['signupPassword'], $postData['signupMail']);

                        if ($return == MSession::REGISTER_SUCCESS) {
                            $profile = new Profile();
                            $profile->active = 1;
                            $profile->user_id = MSession::getInstance()->getUserID();
                            $profile->save();

                            __redirect(__route('ml-microblog'));
                        }
                    } catch (Exception $ex) {
                        $form->addError($ex->getMessage());
                        $form->bindData($this->getRequest()->getPost());
                    }
                } else {
                    $form->bindData($this->getRequest()->getPost());
                }
            }
            $this->render('signup', array('form' => $form));
        }
    }

}
?>
